package a_doubleWrite;

import java.util.Arrays;

/**
 * todo
 *
 * @author luolidong
 * @version 1.0
 * @create 2021-08-02 20:12
 */
public class L287 {
    public int findDuplicate(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] != i + 1) {

                if (nums[nums[i] - 1] == nums[i]) {
                    return nums[i];
                } else {
                    int temp = nums[nums[i] - 1];
                    nums[nums[i] - 1] = nums[i];
                    nums[i] = temp;
                }
            }
        }
        return nums[nums.length - 1];
    }

    public static void main(String[] args) {
        L287 l287 = new L287();
        int duplicate = l287.findDuplicate(new int[]{1, 3, 4, 2, 2});
        System.out.println(duplicate);
    }
}
